package com.weipai.springboot.module.timmer;

import com.weipai.springboot.module.service.DemandService;
import com.weipai.springboot.module.service.GiftCertificateService;
import com.weipai.springboot.module.service.GoodsService;
import com.weipai.springboot.module.service.OrderService;
import com.weipai.springboot.module.service.impl.OrderServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;

/**
 * @Author: 曹朋飞
 * @Date: 2018/10/31 11:26
 * 
 * 定时任务类
 */
@Component
public class OrderTimer {

    private static final Logger log = LoggerFactory.getLogger(OrderTimer.class);
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private GiftCertificateService giftCertificateService;
    @Autowired
    private DemandService demandService;

    /**
     * 每一小时执行一次，更改订单的状态为交易成功
     */
    //@Scheduled(fixedDelay=10000)
    @Scheduled(cron = "0 0 * * * ?")
    public void disposeOrder(){
        try {
            orderService.disposeOrder();
        } catch (Exception e) {
            log.debug(" orderTimer class get deliveryTime error : {}"+e.toString());
        }
    }

    /**
     * 每一小时执行一次，修改用户可提现金额
     */
    //@Scheduled(fixedDelay=10000)
    @Scheduled(cron = "0 0 * * * ?")
    public void disposeMoney(){
        try {
            orderService.disposeMoney();
        } catch (Exception e) {
            log.debug(" orderTimer class get hcashMoney error : {}"+e.toString());
        }
    }

    /**
     * 每一小时执行一次，修改技工可提现金额
     */
    //@Scheduled(fixedDelay=10000)
    @Scheduled(cron = "0 0 * * * ?")
    public void artisanMoney(){
        try {
            demandService.artisanMoney();
        } catch (Exception e) {
            log.debug(" orderTimer class get hcashMoney error : {}"+e.toString());
        }
    }



    /**
     * 每小时执行一次，处理未支付的订单
     */
    //@Scheduled(fixedDelay=10000)
    @Scheduled(cron = "0 0 * * * ?")
    public void disposeUnpaidOrder(){
        orderService.disposeUnpaidOrder();
    }

    /**
     * 每小时执行一次，处理未支付的需求订单
     */
    //@Scheduled(fixedDelay=10000)
    @Scheduled(cron = "0 0 * * * ?")
    public void disposeUnpaidDemand(){
        orderService.disposeUnpaidDemand();
    }

    /**
     * 每天凌晨两点触发事件
     */
    @Scheduled(cron = "0 0 2 * * *")
    public void disposeGiftCertificate(){
        giftCertificateService.disposeGiftCertificate();
    }


    /**
     * 每天凌晨两点触发事件
     */
    @Scheduled(cron = "0 0 2 * * *")
    public void updateSoldOutGoods(){
        goodsService.updateSoldOutGoods();
    }
}
